home *** CD-ROM | disk | FTP | other *** search
- package de.trantor.mail;
-
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
-
- public class ImapClient implements InboxClient {
- private Connection socket;
- private InputStream input;
- private OutputStream output;
- private boolean debug = false;
- private int commandCount = 0;
-
- public void open(String host, String user, String pass) throws Exception, IOException, Pop3Exception {
- if (this.connected()) {
- this.close();
- }
-
- this.socket = Connection.createConnection();
- this.socket.open(host, 143);
- this.input = this.socket.getInputStream();
- this.output = this.socket.getOutputStream();
-
- try {
- this.execute("LOGIN", user + " " + pass, (Message)null);
- this.execute("SELECT", "INBOX", (Message)null);
- } catch (Pop3Exception var5) {
- this.socket.close();
- this.input = null;
- this.output = null;
- this.socket = null;
- throw var5;
- }
- }
-
- public void close() throws IOException, Pop3Exception {
- if (this.connected()) {
- this.execute("CLOSE", "", (Message)null);
- this.execute("LOGOUT", "", (Message)null);
- }
-
- this.socket.close();
- this.input = null;
- this.output = null;
- this.socket = null;
- }
-
- public boolean connected() {
- return this.socket != null;
- }
-
- private void send(String s) throws IOException, Pop3Exception {
- if (this.debug) {
- System.out.println("[IMAP/SEND] " + s);
- }
-
- this.output.write(s.getBytes());
- this.output.write(13);
- this.output.write(10);
- }
-
- private String receive() throws IOException, Pop3Exception {
- StringBuffer buffer = new StringBuffer("");
-
- char c;
- do {
- c = (char)this.input.read();
- if (c != -1 && c != '\r' && c != '\n') {
- buffer.append(c);
- }
- } while(c != '\r');
-
- if (this.debug) {
- System.out.println("[IMAP/RECV] " + buffer);
- }
-
- return new String(buffer);
- }
-
- private String execute(String command, String arguments, Message message) throws IOException, Pop3Exception {
- String result = null;
- String tag = "A" + this.commandCount++ + " ";
- this.send(tag + command + " " + arguments);
-
- String temp;
- for(temp = this.receive(); !temp.startsWith(tag); temp = this.receive()) {
- if (temp.indexOf(" " + command + " ") != -1) {
- int p = temp.indexOf(40);
- int q = temp.indexOf(41, p + 1);
- if (p != -1) {
- if (q > p) {
- result = temp.substring(p + 1, q);
- } else if (message != null) {
- int left = temp.indexOf(123);
- int right = temp.indexOf(125, left);
- this.receiveMessage(message, Integer.parseInt(temp.substring(left + 1, right)));
- }
- }
- }
- }
-
- temp = temp.substring(tag.length());
- if (!temp.startsWith("BAD ") && !temp.startsWith("NO ")) {
- return result;
- } else {
- throw new Pop3Exception(temp);
- }
- }
-
- public int getMessageCount() throws IOException, Pop3Exception {
- String buffer = this.execute("STATUS", "INBOX (MESSAGES)", (Message)null);
- int space = buffer.indexOf(32);
- return Integer.parseInt(buffer.substring(space + 1));
- }
-
- private void receiveMessage(Message message, int size) throws IOException, Pop3Exception {
- int count = 0;
- String buffer = this.receive();
-
- int octets;
- for(octets = buffer.length() + 2; !buffer.equals(""); octets = octets + buffer.length() + 2) {
- if (!buffer.startsWith(" ") && !buffer.startsWith("\t")) {
- message.addHeaderLine(buffer);
- ++count;
- } else {
- message.setHeaderLine(count - 1, message.getHeaderLine(count - 1) + "\r\n" + buffer);
- }
-
- buffer = this.receive();
- }
-
- while(octets < size) {
- buffer = this.receive();
- octets = octets + buffer.length() + 2;
- message.addBodyLine(buffer);
- }
-
- }
-
- public Message getMessage(int index) throws IOException, Pop3Exception {
- Message message = new Message();
- this.execute("FETCH", index + 1 + " (RFC822)", message);
- return message;
- }
-
- public Message getHeaders(int index) throws IOException, Pop3Exception {
- Message message = new Message();
- this.execute("FETCH", index + 1 + " (RFC822.HEADER)", message);
- return message;
- }
-
- public void removeMessage(int index) throws IOException, Pop3Exception {
- this.execute("STORE", index + 1 + " +FLAGS.SILENT (\\DELETED)", (Message)null);
- }
-
- public void setDebug(boolean debug) {
- this.debug = debug;
- }
-
- public boolean getDebug() {
- return this.debug;
- }
- }
-